package com.me.study;

/**
 * @Title: com.me.Randomm
 * @author me
 * @date 2016年3月30日 下午10:16:11
 *
 *       <p>
 *       Description: 这是一个
 */

import java.util.*;

/** 根据概率获取奖品的测试类 */
public class TestRandom {

    public static void main(String[] args) {
//		TestRandom tr = new TestRandom();
//		// 用List随机
//		long begin = System.currentTimeMillis();
//		System.err.println("list开始");
//		for (int i = 0; i < 100; i++) {
//			tr.getGumByList();
//		}
//		System.err.println("list使用时间：" + (System.currentTimeMillis() - begin));
//		// 用set随机
//		long begin1 = System.currentTimeMillis();
//		System.err.println("set开始");
//		for (int i = 0; i < 100; i++) {
//			tr.getGumBySet();
//		}
//		System.out.println("set使用时间：" + (System.currentTimeMillis() - begin1));

        double baseNum = 0.99;
        String product = "矿泉水,打火机,雨伞,指甲刀";
        String num = "2,2,2,1";
        String[] p = product.split(",");
        String[] n = num.split(",");

        java.util.Random ran = new java.util.Random();
        double base = ran.nextDouble();

        if (base > baseNum) {
            System.err.println("没中奖");
            return;
        }

        List<String> list = new ArrayList<String>();
        for (int i = 0; i < p.length; i++) {
            for (int j = 0; j < Integer.parseInt(n[i]); j++) {
                list.add(p[i]);
            }
        }
        // TODO 打乱奖池数据
        int l = ran.nextInt(list.size());
        System.err.println(list.get(l));
    }

    /* Set 获取 */
    void getGumBySet() {
        int random = 20;
//    int random = new Random().nextInt(100);
        int prizeRate = 0;// 中奖率
        Iterator<Gem> it = initSetGems().iterator();
        while (it.hasNext()) {
            Gem gem = it.next();
            prizeRate += gem.getPriority();
            if (random < prizeRate) {
                System.out.println("奖品为："
                        + gem.getName()
                        + "  概率："
                        + gem.getPriority()
                        + " 随机数random："
                        + random
                        + " 概率基数prizeRate："
                        + prizeRate);
                break;
            }
        }
    }

    /** List 获取 */
    void getGumByList() {
        int random = 20;
//      int random = new Random().nextInt(100);
        int prizeRate = 0;// 中奖率
        Iterator<Gem> it = initGems().iterator();
        while (it.hasNext()) {
            Gem gem = it.next();
            prizeRate += gem.getPriority();
            if (random < prizeRate) {
                System.out.println("奖品为："
                        + gem.getName()
                        + "  概率："
                        + gem.getPriority()
                        + " 随机数random："
                        + random
                        + " 概率基数prizeRate："
                        + prizeRate);
                break;
            }
        }
    }

    /** 初始化list */
    List<Gem> initGems() {
        List<Gem> gums = new ArrayList<Gem>();
        gums.add(new Gem("A", 20));
        gums.add(new Gem("B", 7));
        gums.add(new Gem("C", 30));
        gums.add(new Gem("D", 40));
        gums.add(new Gem("E", 3));
        return gums;
    }

    /** 初始化set */
    Set<Gem> initSetGems() {
        Set<Gem> gums = new HashSet<Gem>();
        gums.add(new Gem("A", 20));
        gums.add(new Gem("B", 7));
        gums.add(new Gem("C", 30));
        gums.add(new Gem("D", 40));
        gums.add(new Gem("E", 3));
        return gums;
    }

    /** 奖品类 */
    class Gem {
        /** 奖品名称 */
        private String name;
        /** 奖品概率 */
        private int priority;

        public Gem() {
            super();
        }

        public Gem(String name, int priority) {
            super();
            this.name = name;
            this.priority = priority;
        }

        @Override
        public String toString() {
            return "Gum [name=" + name + ", priority=" + priority + "]";
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getPriority() {
            return priority;
        }

        public void setPriority(int priority) {
            this.priority = priority;
        }
    }
}
